package club.pchome.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import club.pchome.common.constant.SystemConstant;
import club.pchome.util.DateUtil;
import club.pchome.util.JsoupSignAdm;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.session.JdbcSessionDataSourceInitializer;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.alibaba.fastjson.JSON;

import club.pchome.model.InterfaceDto;
import club.pchome.service.InterfaceService;

public abstract class BaseController {
	Logger logger = LoggerFactory.getLogger(getClass());
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;

	@Autowired
	InterfaceService interfaceService;

	@Autowired
	JsoupSignAdm signAdm;

	public String errorUrl;// 错误返回url

	// 页面表单存放数据
	protected Map<String, String> paramMap = new HashMap<String, String>();

	//存储所有用户的cookie
	protected static HashMap<String, String> hashMap = new HashMap<String, String>();

	/**
	 * 以MAP的格式，接收所有参数
	 * @return
	 */
	protected Map<String, String> getRequestMap() {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<String> enumeration = this.request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String name = enumeration.nextElement();
			map.put(name, request.getParameter(name));
		}
		return map;
	}
	/**
	 * 数据流类型输出
	 * @param str
	 */
	public void toWrite(String str) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter pw = response.getWriter();
			pw.write(str);
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * JOSN类型输出
	 * @param obj
	 */
	protected void writeJson(Object obj) {
		String jsonString = JSON.toJSONString(obj); 
		logger.info("json输出数据==>" + jsonString);
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/x-json;charset=UTF-8");
			PrintWriter pw = response.getWriter();
			pw.write(jsonString);
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}


	public Map<String, String> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, String> paramMap) {
		this.paramMap = paramMap;
	}

	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
		this.request = request;
		this.response = response;
		this.session = request.getSession();
	}

	/**记录接口使用数据*
	 * @param useNamePhone 使用人手机
	 * @param interfaceName 使用接口名称
	 * @return
	 */
	public int interfaceTotal(String useNamePhone , String interfaceName) {
		InterfaceDto record = new InterfaceDto();
		record.setUsename(useNamePhone);
		record.setInterfacename(interfaceName);
		return interfaceService.insert(record);
	}


	/***
	 * 登录通用
	 * @param phone
	 * @param passwd
	 * @return
	 */
	public String ADMLOGIN(String phone , String passwd){
		phone = phone.replaceAll("\\s*", "");
		passwd = passwd.replaceAll("\\s*", "");
		String userInfo = "{\"loginName\":\"" + phone +"\",\"pwd\":\""+ passwd +"\"}";
		//获取cookie
		String JSESSIONID = hashMap.get(phone);
		if(JSESSIONID == null) {
			JSESSIONID = signAdm.AdmLogin(userInfo);
			if(JSESSIONID.equals("10021")){
				return JSESSIONID;
			}else{
				hashMap.put(phone, JSESSIONID);
			}
		}else{
			String isOpenDirect = signAdm.JsoupToDataByCookie(SystemConstant.isOpenDirect, JSESSIONID);
			JSONObject jsonObject = JSONObject.parseObject(isOpenDirect);
			int code = jsonObject.getInteger("code");
			if(code == 100109) {
				//重新登陆获取COOKIE
				JSESSIONID = signAdm.AdmLogin(userInfo);
				if(JSESSIONID.equals("10021")){
					return JSESSIONID;
				}else{
					hashMap.put(phone, JSESSIONID);
				}
			}
		}
		return JSESSIONID;
	}


	/**
	 * 格式化编码内容
	 * @param coinId
	 * @return
	 */
	public String getCoinIdName(int coinId){
		String coinIdName = null;
		if (coinId == 1001) { //USDT
			coinIdName = "USDT";
		} else if (coinId == 1002) { // 可售余额
			coinIdName = "可售余额";
		} else if (coinId == 1003) { // 可售额度
			coinIdName = "可售额度";
		} else if (coinId == 1004) { // 矿池钱包
			coinIdName = "矿池钱包";
		} else if (coinId == 1005) { // 矿池资产
			coinIdName = "矿池资产";
		} else if (coinId == 1006) { // 信用积分
			coinIdName = "信用积分";
		} else if (coinId == 1007) { // ADM
			coinIdName = "ADM";
		} else if (coinId == 1008) { // UT
			coinIdName = "UT";
		} else if (coinId == 1009) { // USDT冷钱包
			coinIdName = "USDT冷钱包";
		}

		return coinIdName;
	}

}